package com.hw.one.dam2.dao;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.hw.one.dam2.util.CalendarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.param.SmallAirParam;
import com.hw.one.dam2.util.AQIComparatorMap;

/**
 * 小型站 的 DAO 实现类
 * 
 * @author admin
 *
 */
@Repository(value = "smallAirDao")
public class SmallAirDao implements ISmallAirDao {

	@Autowired
	private MongoTemplate mongoTemplateStandardSiteData;

	// 小型站 分钟数据
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findSmallMinuteDataByPointCodeAndTime(SmallAirParam smallAirParam) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.NO_SPLIT_SDF_YM);
		
		String tableTime = sdf.format(calendar.getTime());
		
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").gte(smallAirParam.getStartTime())
				.lte(smallAirParam.getEndTime());
		criteria.and("SITECODE").in(smallAirParam.getSiteCodes());

		// 创建查询对象
		Query query = new Query(criteria);

		// 增加排序功能
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

		// 查询数据 分钟数据
		List<Map> smallMinuteAirList = mongoTemplateStandardSiteData.find(query, Map.class,
				"T_DATA_SC_MIN_S_COLLECT-" + tableTime);

		return smallMinuteAirList;
	}

	// 小型站 小时数据
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findSmallHourDataByPointCodeAndTime(SmallAirParam smallAirParam) {
		Calendar calendar = Calendar.getInstance();
		int monthNum = calendar.get(Calendar.MONTH);
		int yearNum = calendar.get(Calendar.YEAR);
		String tableTime = null;
		
		// 如果是1月到6月，则表名称是年+a
		if(monthNum<=5){
			tableTime = yearNum + "a";
		}else {
			tableTime = yearNum + "b";
		}
		
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").gte(smallAirParam.getStartTime())
				.lte(smallAirParam.getEndTime());
		criteria.and("SITECODE").in(smallAirParam.getSiteCodes());

		// 创建查询对象
		Query query = new Query(criteria);

		// 增加排序功能
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

		// 查询数据 小时数据
		List<Map> smallHourAirList = mongoTemplateStandardSiteData.find(query, Map.class,
				"T_DATA_SC_HOUR_S_COLLECT-" + tableTime);

		return smallHourAirList;
	}

	// 小型站 日数据
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findSmallDayDataByPointCodeAndTime(SmallAirParam smallAirParam) {
		
		
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").gte(smallAirParam.getStartTime())
				.lte(smallAirParam.getEndTime());
		criteria.and("SITECODE").in(smallAirParam.getSiteCodes());

		// 创建查询对象
		Query query = new Query(criteria);

		// 增加排序功能
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));

		// 查询数据 日数据
		List<Map> smallDayAirList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_SC_DAY_S_COLLECT");

		return smallDayAirList;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findLast12HourSmallSiteHourDataBySiteCode(String siteCode) {
		Calendar calendar = Calendar.getInstance();
		int monthNum = calendar.get(Calendar.MONTH);
		int yearNum = calendar.get(Calendar.YEAR);

		String tableTime = null;
		// 如果是1月到6月，则表名称是年+a
		if(monthNum<=5){
			tableTime = yearNum + "a";
		}else {
			tableTime = yearNum + "b";
		}


		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("SITECODE").is(siteCode);
		// 获取最新记录
		List<Map> newDataList = mongoTemplateStandardSiteData.find(
				new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME")).limit(12), Map.class,
				"T_DATA_SC_HOUR_S_COLLECT-" + tableTime);

		Collections.reverse(newDataList);
		return newDataList;
	}

	@Override
	public List<Map> findSmallAirNewMinuteDataBySiteCodeList(List<String> siteCodeList) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.NO_SPLIT_SDF_YM);

		String tableTime = sdf.format(calendar.getTime());
		String endTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.SDF_YMD_HMS);
		calendar.add(Calendar.MINUTE, -30);
		String startTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.SDF_YMD_HMS);
		// 获取最新记录
//		Map newData = mongoTemplateStandardSiteData.findOne(
//				new Query(Criteria.where("SITECODE").in(siteCodeList)).with(new Sort(Sort.Direction.DESC, "MONITORTIME")), Map.class,
//				"T_DATA_SC_MIN_S_COLLECT-" + tableTime);
//		// 得到最新时间
//		Object timeObj = newData.get("MONITORTIME");
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("SITECODE").in(siteCodeList).and("MONITORTIME").gte(startTime).lte(endTime);

		// 创建查询对象
		Query query = new Query(criteria)
				.with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
		List<Map> newDtaList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_SC_MIN_S_COLLECT-" + tableTime);
		return newDtaList;
	}

	@Override
	public Map findSmallAirNewMinuteOneDataBySiteCode(String siteCode) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.NO_SPLIT_SDF_YM);

		String tableTime = sdf.format(calendar.getTime());
		String startTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.SDF_YMD_HMS);
		// 获取最新记录
		Map newData = mongoTemplateStandardSiteData.findOne(
				new Query(Criteria.where("SITECODE").is(siteCode)).with(new Sort(Sort.Direction.DESC, "MONITORTIME")), Map.class,
				"T_DATA_SC_MIN_S_COLLECT-" + tableTime);
		return newData;
	}

}
